<?php

namespace app\controllers;
use yii\web\Controller;
use yii\db\Query;
use app\models\UploadForm;
use yii\web\UploadedFile;




require_once str_replace("/web","",$_SERVER['DOCUMENT_ROOT']).'/vendor/workerman/gatewayclient/Gateway.php';
use GatewayClient\Gateway;


class ServerController extends Controller{

    public $layout = false;
    public $enableCsrfValidation = false;

   
    
    //上传文件
    public function actionUpload(){
      if ($this->post()){
          $model = new UploadForm();
              $model->imageFile = $model->imageFile=UploadedFile::getInstanceByName('file');
              $res=$model->upload();
              if ($res['code']) {
                  // 文件上传成功
                  return json_encode(['code'=>1,'url'=>'https://'.$_SERVER['HTTP_HOST'].$res['url']]);
              }else{
                  return json_encode(['code'=>0,'上传失败']);
              }
      }
    }
    
         //发送消息
    public function actionSend(){
    	         if ($this->post()){
             $data=\Yii::$app->request->post();
         Gateway::$registerAddress = '127.0.0.1:1240';
        $res=Gateway::sendToUid($data['to'],json_encode($data));
             $data['create_time']=time();
             $res=\Yii::$app->db->createCommand()->insert('yii_chat',$data)->execute();
             if ($res){
                 return json_encode(['code'=>1,'msg'=>'发送成功']);
             }else{
                 return json_encode(['code'=>0,'msg'=>'发送失败']);
             }
         }
    	
    }
    
    
     //查询过往记录
    public function actionChatlog(){
    	  $data=\Yii::$app->request->post();
    	  
		$connection  = \Yii::$app->db;
    	  if($data['from']==$data['to']){
    	  	$command = $connection->createCommand("SELECT * FROM `yii_chat` WHERE ( `from` = ".$data['from'].") AND ( `to` = ".$data['to'].") order by create_time desc");
    	 
    	  }else{
    	 $command = $connection->createCommand("SELECT * FROM `yii_chat` WHERE ( `from` = ".$data['from']." OR `to` = ".$data['from']." ) AND ( `to` = ".$data['to']." OR `from` = ".$data['to']." ) order by create_time desc");
    	  }
    	
		$res= $command->queryAll($sql);

    	   return json_encode(['code'=>1,'msg'=>$res]);
    	
    }
    
        //获取所以联系人
    public function actionUserlist(){
    	   $list=(new Query())->select('*')->from('yii_chat')->groupBy(['from'])->orderBy('create_time desc')->all();
    	   return json_encode(['code'=>1,'msg'=>$list]);
    	
    }
    
    
    //常用语添加
    public function actionAdd(){
    	    if ($this->post()){
             $data=\Yii::$app->request->post();
             $res=\Yii::$app->db->createCommand()->insert('yii_info',$data)->execute();
             if ($res){
                 return json_encode(['code'=>1,'msg'=>'添加成功']);
             }else{
                 return json_encode(['code'=>0,'msg'=>'添加失败']);
             }
         }
    	
    }

    //常用语查看
    public function actionLook(){
    	  $id=$this->GetId();
    	  $list=(new Query())->select('*')->from('yii_info')->where(array('id'=>$id))->one();
    	   return json_encode(['code'=>1,'msg'=>$list]);
    }

    //常用语编辑
    public function actionEdit(){
    	    if ($this->post()){
             $data=\Yii::$app->request->post();
             $res=\Yii::$app->db->createCommand()->update('yii_info',$data,array('id'=>$data['id']))->execute();
             if ($res){
                 return json_encode(['code'=>1,'msg'=>'更新成功']);
             }else{
                 return json_encode(['code'=>0,'msg'=>'无数据更新']);
             }
         }
    	
    }

    //常用列表删除
    public function actionDel(){
    	$id=$this->PostId();
    	$res=\Yii::$app->db->createCommand()->delete('yii_info',array('id'=>$id))->execute();
    	           if ($res){
                 return json_encode(['code'=>1,'msg'=>'删除成功']);
             }else{
                 return json_encode(['code'=>0,'msg'=>'删除失败']);
             }
    }


    //常用语列表
    public function actionInfolist(){
    	$list=(new Query())->select('*')->from('yii_info')->orderBy('id desc')->all();
    	   return json_encode(['code'=>1,'msg'=>$list]);
    }


    //常用语排序
    public function actionDosort(){
    	  $id=$this->PostId();
    	  $info=(new Query())->select('*')->from('yii_info')->where(array('id'=>$id))->one();//要移动的
    	  $change=(new Query())->select('*')->from('yii_info')->where(['>', 'id', $id])->orderBy('id asc')->one();//被移动的
    	  
    	  
    	  
    	  $data['id']=$change['id'];
    	  $data['info']=$info['info'];
    	
    	  $data2['id']=$info['id'];
    	  $data2['info']=$change['info'];
    	  \Yii::$app->db->createCommand()->delete('yii_info',array('id'=>$info['id']))->execute();
    	  \Yii::$app->db->createCommand()->delete('yii_info',array('id'=>$change['id']))->execute();
    	  
    	  \Yii::$app->db->createCommand()->insert('yii_info',$data)->execute();
    	  \Yii::$app->db->createCommand()->insert('yii_info',$data2)->execute();
    	  
    	       return json_encode(['code'=>1,'msg'=>'已上移']);
    }
    
    
    
    
        //post请求
    public function post(){
        if (\Yii::$app->request->isPost){
            return true;
        }else{
            return false;
        }
    }

    //获取Post请求ID
    public function PostId(){
        $id=(int)\Yii::$app->request->post()['id'];
        return $id;
    }

    //获取Get请求ID
    public function GetId(){
        $id=(int)\Yii::$app->request->get()['id'];
        return $id;
    }


 //   //小程序配置
    //  public function actionIndex(){
    //      if($this->post()){
    //      $data=\Yii::$app->request->post();
    //      $res=\Yii::$app->db->createCommand()->update('yii_wxapp',$data)->execute();
    //      if ($res){
    //          return json_encode(['code'=>1,'msg'=>'更新成功']);
    //      }else{
    //          return json_encode(['code'=>0,'msg'=>'无数据更新']);
    //         }
    //      }
    //      $wxapp=(new Query())->select('*')->from('yii_wxapp')->one();
    //      return $this->render('index',['wxapp'=>$wxapp]);
    //   }

    // //轮播图
    // public function actionBanner(){
    //      $list=(new Query())->select('*')->from('yii_banner')->orderBy('sort')->all();
    //      foreach ($list as $k=>$v){
    //          $list[$k]['img']=$this->GetImg($v['img_id']);
    //      }
    //      return $this->render('banner',['list'=>$list]);
    // }

    // //添加轮播图
    // public function actionAddbanner(){
    //      if ($this->post()){
    //          $data=\Yii::$app->request->post();
    //          unset($data['imageFile']);
    //          $res=\Yii::$app->db->createCommand()->insert('yii_banner',$data)->execute();
    //          if ($res){
    //              return json_encode(['code'=>1,'msg'=>'添加成功']);
    //          }else{
    //              return json_encode(['code'=>0,'msg'=>'添加失败']);
    //          }
    //      }
    //  return $this->render('addbanner');
    // }

    // //编辑轮播图
    // public function actionEditbanner(){
    //     if ($this->post()){
    //         $data=\Yii::$app->request->post();
    //         unset($data['imageFile']);
    //         $res=\Yii::$app->db->createCommand()->update('yii_banner',$data,array('id'=>$data['id']))->execute();
    //         if ($res){
    //             return json_encode(['code'=>1,'msg'=>'更新成功']);
    //         }else{
    //             return json_encode(['code'=>0,'msg'=>'无数据更新']);
    //         }
    //     }else{
    //         $id=$this->GetId();
    //         $list=(new Query())->select('*')->from('yii_banner')->where(array('id'=>$id))->one();
    //         $list['img']=$this->GetImg($list['img_id']);
    //     }

    //     return $this->render('editbanner',['list'=>$list]);
    // }

    // //删除轮播图
    // public function actionDelbaner(){
    //      $id=$this->PostId();
    //      $transaction = \Yii::$app->db->beginTransaction();
    //      try {
    //          $info=(new Query())->select('*')->from('yii_banner')->where(array('id'=>$id))->one();
    //           $this->DelFile($info['img_id']);
    //           $this->DelPicture($info['img_id']);
    //          \Yii::$app->db->createCommand()->delete('yii_banner',array('id'=>$id))->execute();
    //          $transaction->commit();
    //          return json_encode(['code'=>1,'msg'=>'删除成功']);
    //      } catch (\Exception $e) {
    //         $transaction->rollBack();
    //          return json_encode(['code'=>0,'msg'=>'删除失败']);
    //     }
    // }


}
